package com.haoyeteng.sxbd.core.shiro.factory;


import java.util.List;

import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.haoyeteng.sxbd.core.common.constant.factory.ConstantFactory;
import com.haoyeteng.sxbd.core.common.constant.state.ManagerStatus;
import com.haoyeteng.sxbd.core.shiro.ShiroAdmin;
import com.haoyeteng.sxbd.core.util.SpringContextHolder;
import com.haoyeteng.sxbd.modular.system.dao.MenuMapper;
import com.haoyeteng.sxbd.modular.system.dao.AdminMapper;
import com.haoyeteng.sxbd.modular.system.model.Admin;

@Service
@DependsOn("springContextHolder")
@Transactional(readOnly = true)
public class ShiroFactroy implements IShiro {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private MenuMapper menuMapper;

    public static IShiro me() {
        return SpringContextHolder.getBean(IShiro.class);
    }

    @Override
    public Admin admin(String account) {

        Admin admin = adminMapper.getByAccount(account);

        // 账号不存在
        if (null == admin) {
            throw new CredentialsException();
        }
        // 账号被冻结
        if (admin.getStatus() != ManagerStatus.OK.getCode()) {
            throw new LockedAccountException();
        }
        return admin;
    }

    @Override
    public ShiroAdmin shiroAdmin(Admin admin) {
        ShiroAdmin shiroAdmin = new ShiroAdmin();

        shiroAdmin.setId(admin.getId());
        shiroAdmin.setAccount(admin.getAccount());
        //shiroAdmin.setDeptId(admin.getDeptid());
        //shiroAdmin.setDeptName(ConstantFactory.me().getDeptName(admin.getDeptid()));
        //shiroAdmin.setName(admin.getName());

        Integer roleId = admin.getRoleid();
        String roleName = ConstantFactory.me().getSingleRoleName(roleId);
        shiroAdmin.setRoleId(roleId);
        shiroAdmin.setRoleName(roleName);
        /*List<Integer> roleList = new ArrayList<Integer>();
        List<String> roleNameList = new ArrayList<String>();
        for (int roleId : roleArray) {
            roleList.add(roleId);
            roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
        }
        shiroAdmin.setRoleList(roleList);
        shiroAdmin.setRoleNames(roleNameList);*/

        return shiroAdmin;
    }

    @Override
    public List<String> findPermissionsByRoleId(Integer roleId) {
        return menuMapper.getResUrlsByRoleId(roleId);
    }

    @Override
    public String findRoleNameByRoleId(Integer roleId) {
        return ConstantFactory.me().getSingleRoleTip(roleId);
    }

    @Override
    public SimpleAuthenticationInfo info(ShiroAdmin shiroAdmin, Admin admin, String realmName) {
        String credentials = admin.getPassword();

        // 密码加盐处理
        String source = admin.getSalt();
        ByteSource credentialsSalt = new Md5Hash(source);
        return new SimpleAuthenticationInfo(shiroAdmin, credentials, credentialsSalt, realmName);
    }

}
